﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using com.lover.astd.common.config;
using com.lover.astd.common.manager;
using com.lover.astd.common.model;
using com.lover.astd.common;

namespace com.lover.astd.game.ui.server.impl.equipment
{
    public class WeaponServer : LogicServer
    {
        public WeaponServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_Weapon; ServerReadableName = ConfigStrings.SR_Weapon; }
        public override void renderSettings()
        {
            MainForm frm = _mainForm;
            Dictionary<string, string> conf = getConfig(ServerName);
            frm.chk_upgrade_weapone_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");

            _mainForm.txt_weaponinfo.Clear();

            string text = getUser()._weapon_info;
            string[] text_lines = text.Split('/');
            for (int i = 0, len=text_lines.Length; i < len; i++)
            {
                string item = text_lines[i];
                string[] color_text = item.Split('|');
                if (color_text.Length == 1) renderText(item);
                else renderText(color_text[1], color_text[0], i < len - 1);
            }
        }
        private void renderText(string text, string colorstr="black", bool addEnter=true)
        {
            Color color = System.Drawing.Color.Black;
            if (colorstr == "gray") color = System.Drawing.Color.Gray;
            else if (colorstr == "blue") color = System.Drawing.Color.Blue;
            else if (colorstr == "green") color = System.Drawing.Color.Green;
            else if (colorstr == "yellow") color = System.Drawing.Color.Yellow;
            else if (colorstr == "red") color = System.Drawing.Color.Red;
            else if (colorstr == "purple") color = System.Drawing.Color.Purple;
            RichTextBox ctrl = _mainForm.txt_weaponinfo;
            ctrl.AppendText(text);
            ctrl.Select(ctrl.Text.Length - text.Length, text.Length);
            ctrl.SelectionColor = color;
            ctrl.Select(ctrl.Text.Length, 0);
            if(addEnter) ctrl.AppendText("\n");
            ctrl.ScrollToCaret();
        }
        public override void saveSettings()
        {
            MainForm frm = _mainForm;
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_upgrade_weapone_enable.Checked.ToString());
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            string weapon = "upgradeweapon";
            conf.setConfig(weapon, ConfigStrings.enabled, "true");
            renderSettings();
        }
    }
}
